import axios from 'axios';
import {showNotify} from "vant";
import configBase from './configBase'
import store from "../store/index.js";

const Toast = (message, type = 1) => {
    let msgType;
    switch (type) {
        case 1:
            msgType = 'success'
            break;
        case 2:
            msgType = 'warning'
            break;
        case 3:
            msgType = 'primary'
            break;
        default:
            msgType = 'danger'
    }
    showNotify({
        type: msgType,
        message: message,
        duration: 2000
    })
}
// 默认超时设置
axios.defaults.timeout = 50000;

// 相对路径设置
axios.defaults.baseURL = (process.env.NODE_ENV === 'development' ? "/" : configBase.baseURL) + configBase.version;

//http request 拦截器
axios.interceptors.request.use(
    config => {
        const token =store.state.user.token;

        const lang = localStorage.getItem('lang');
        if (!config.headers['Content-Type']) {
            config.headers = {
                'Content-Type': 'application/json',
            };
        }
        if (token) {
            config.headers.Authorization = token
        }
        if (lang) {
            config.headers.lang = lang
        }
        if (config.method === 'get') {
            config.params = config.params || {};
            config.params = JSON.parse(JSON.stringify(config.params));
        } else {
            config.data = config.data || {};
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    }
);

//http response 拦截器
axios.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        return Promise.reject(error)
    }
)
;

/**
 * 封装get方法
 * @param url
 * @param params
 * @returns {Promise}
 */
export function fetch(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        })
            .then(response => {
                if (response.data.code === 200) {
                    //返回成功处理  这里传的啥 后续调用的时候 res就是啥
                    resolve(response.data.data);//我们后台所有数据都是放在返回的data里所以这里统一处理了
                } else {
                    console.log(response)
                    //错误处理
                    showNotify(response.data.message)
                }
            })
            .catch(err => {
                reject(err);
                // var message = 'Request failed! Please check the network';
                // //错误返回
                // message = err.response ? err.response.data.message : message;
                // console.log(message)
                // showNotify(message)
            })
    })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                if (response.data.code === 200) {
                    // showNotify(response.data.message)
                    resolve(response.data.data);
                } else if (response.data.code === 4001) {
                    store.commit('user/logout')
                } else {
                    showNotify(response.data.message)
                    reject()
                }
            }, err => {
                reject(err);
                // var message = 'Request failed! Please check the network';
                // //错误返回
                // // message =err.response? err.response.data.message:message;
                // console.log(err.response)
                // showNotify(message)
            })
    })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                if (response.data.code === 200) {
                    resolve(response.data.data);
                } else {
                    showNotify(response.data.message)
                }
            }, err => {
                reject(err);
                // let message = 'Request failed! Please check the network';
                // if (err.response) message = err.response.data.message;
                // showNotify(message)
            })
    })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                if (response.data.code === 200) {
                    resolve(response.data.data);
                } else {
                    showNotify(response.data.message)
                }
            }, err => {
                reject(err);
                // let message = 'Request failed! Please check the network';
                // if (err.response) message = err.response.data.message;
                // showNotify(message)
            })
    })
}

export function del(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.delete(url, data)
            .then(response => {
                if (response.data.code === 200) {
                    resolve(response.data.data);
                } else {
                    showNotify(response.data.msg)
                }
            }, err => {
                reject(err);
                // let message = 'Request failed! Please check the network';
                // if (err.response) message = err.response.data.message;
                // showNotify(message)
            })
    })
}
